<template>
  <!-- 组织架构 -->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools :tree-node="company">
          <el-dropdown-item :disabled="!checkPermission('add-depts')" command="add" @click.native="addDepts(company)">添加子部门</el-dropdown-item>
        </TreeTools>

        <el-tree
          :data="departs"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDept="getDepartments"
          >
            <el-dropdown-item command="add" :disabled="!checkPermission('add-depts')" @click.native="addDepts(data)">添加子部门</el-dropdown-item>
            <el-dropdown-item command="edit" @click.native="editDepts(data)">编辑部门</el-dropdown-item>
            <el-dropdown-item
              @click.native="deldept(data.id)"
            >删除部门</el-dropdown-item>
          </TreeTools>

        </el-tree>
      </el-card>
      <!-- 放置新增弹层组件  -->
      <add-dept v-if="showDialog" ref="adddept" :show-dialog.sync="showDialog" :tree-node="node" @addDept="addDept" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept' // 引入新增部门组件
import { getDepartments, delDepartments } from '@/api/department.js'
// import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    TreeTools, // 树形结构
    AddDept// 新增部门
  },
  props: {},
  data() {
    return {
      company: { },
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 显示窗体
      node: {}// 当前点击的dom节点
    }
  },
  async created() {
    this.getDepartments()
  },
  mounted() {},
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取部门
    async getDepartments() {
      const data = await getDepartments()
      this.company = { name: data.companyName, manager: data.companyManager || '负责人', id: '' }
      // this.departs = data.depts
      // 第一种方式
      data.depts.forEach(element => {
        const children = data.depts.filter((item) => {
          return item.pid === element.id
        })
        if (children && children.length > 0) {
          element.children = children
        }
      })
      const depts = data.depts.filter(t => t.pid === '')
      this.departs = depts
    // 第二种方式
    // this.departs = tranListToTreeData(data.depts)
    },
    // 获取当前点击项的数据
    addDepts(node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
    },
    // 新增部门
    addDept() {
      this.getDepartments()
      // this.showDialog = false
    },
    // 编辑部门
    async editDepts(node) {
      this.node = node
      this.showDialog = true
      // const formdata = await getDepartDetail(this.node.id)
      // this.$refs.adddept.formdata = formdata
      this.$nextTick(() => {
        this.$refs.adddept.getDepartDetail(this.node.id)
      })
    },
    // 删除部门
    deldept(id) {
      // 删除部门
      this.$confirm('确认删除自部门?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delDepartments(id)
        this.$message({
          message: '删除成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
        this.getDepartments()
      }).catch(() => {

      })
    }

  }

}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
